﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace VMukti.DataAccess
{
    public class ClsUserDataService : ClsDataServiceBase
    {

        public ClsUserDataService() : base() { }

        ////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///	Creates a new DataService and specifies a transaction with
        ///	which to operate
        /// </summary>
        ////////////////////////////////////////////////////////////////////////
        public ClsUserDataService(IDbTransaction txn) : base(txn) { }


        public DataSet User_GetAll()
        {
            try
            {
                return ExecuteDataSet("Select UserInfo.*,Payroll.* from UserInfo left outer join Payroll on UserInfo.Id=Payroll.UserId where UserInfo.IsDeleted=0;", CommandType.Text, null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--Vmukti.Dataaccess--:--User--:--clsUserDataService.cs--:--User_GetAll()--");
                
                return null;
            }
        }

        public DataSet User_GetByID(int ID)
        {
            try
            {
                return ExecuteDataSet("spGUserInfoPayroll", CommandType.StoredProcedure, CreateParameter("@pID", SqlDbType.Int, ID));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--Vmukti.Dataaccess--:--User--:--clsUserDataService.cs--:--User_GetById()--");
                
                return null;
            }
        }

        public DataSet User_GetByUNamePass(string UName, string Pass, ref bool Result)
        {
            try
            {
                SqlCommand cmd;
                DataSet ds = ExecuteDataSet(out cmd, "spGUserInfoPayroll", CommandType.StoredProcedure,
                    CreateParameter("@pID", SqlDbType.Int, -1),
                    CreateParameter("@pUName", SqlDbType.VarChar, UName),
                    CreateParameter("@pPass", SqlDbType.VarChar, Pass),
                    CreateParameter("@pResult", SqlDbType.Bit, false, ParameterDirection.InputOutput));

                Result = (bool)cmd.Parameters["@pResult"].Value;
                cmd.Dispose();
                return ds;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--Vmukti.Dataaccess--:--User--:--clsUserDataService.cs--:--User_GetByUNamePass()--");
               
                return null;
            }
        }

        public void User_LogOff(Int64 UserID)
        {
            try
            {
                ExecuteDataSet("spDOnlineUsers", CommandType.StoredProcedure,
                    CreateParameter("@pID", SqlDbType.BigInt, -1),
                    CreateParameter("@pUserID", SqlDbType.BigInt, UserID));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--Vmukti.Dataaccess--:--User--:--clsUserDataService.cs--:--User_Logoff()--");
               
            }
        }

        public int User_Save(ref int ID, string displayame,int roleID,string firstName,string lastName,string eMail,string password,bool isActive,int byUserId,float ratePerHour,float overTimeRate,float doubleRatePerHour,float doubleOverTimeRate )
        {
            try
            {
                SqlCommand cmd;
                ExecuteNonQuery(out cmd, "spAEUserInfoPayroll",
                    CreateParameter("@pID", SqlDbType.Int, ID),
                    CreateParameter("@pDisplayName", SqlDbType.NVarChar, displayame, 100),
                    CreateParameter("@pRoleID", SqlDbType.BigInt, roleID),
                    CreateParameter("@pFirstName", SqlDbType.NVarChar, firstName, 50),
                    CreateParameter("@pLastName", SqlDbType.NVarChar, lastName, 50),
                    CreateParameter("@pEMail", SqlDbType.NVarChar, eMail, 256),
                    CreateParameter("@pPassword", SqlDbType.NVarChar, password, 50),
                    CreateParameter("@pIsActive", SqlDbType.Bit, isActive),
                    CreateParameter("@pByUserID", SqlDbType.BigInt, byUserId),
                    CreateParameter("@pRatePerHour", SqlDbType.Float, ratePerHour),
                    CreateParameter("@pOverTimeRate", SqlDbType.Float, overTimeRate),
                    CreateParameter("@pDoubleRatePerHour", SqlDbType.Float, doubleRatePerHour),
                    CreateParameter("@pDoubleOverTimeRate", SqlDbType.Float, doubleOverTimeRate),
                    CreateParameter("@pReturnId", SqlDbType.BigInt, ParameterDirection.Output));
                int retID = int.Parse(cmd.Parameters["@pReturnId"].Value.ToString());

                cmd.Dispose();
                return retID;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--Vmukti.Dataaccess--:--User--:--clsUserDataService.cs--:--User_Save()--");
                return 0;
            }
        }

        public void User_Delete(int ID)
        {
            try
            {
                ExecuteNonQuery("spDUserInfoPayroll", CreateParameter("@pID", SqlDbType.Int, ID));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "VMukti--:--Vmukti.Dataaccess--:--User--:--clsUserDataService.cs--:--User_Delete()--");
             
            }
        }

    }
}
